package Core;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ConfigManager {

    private static ConfigManager _instance;

    public static ConfigManager Instance() {
        if (_instance == null) {
            _instance = new ConfigManager();
        }

        return _instance;
    }

    private String host, remotehost;
    private int port, remoteport;
    private String name;
    private String useheartbeat;
    private int heartbeattime;

    public void loadConfig(String filePath) throws IOException {
        StringBuffer fileData = new StringBuffer();
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }
        reader.close();

        JSONObject jsonConfig = new JSONObject(fileData.toString());
        host = jsonConfig.getString("host");
        port = jsonConfig.getInt("port");
        remotehost = jsonConfig.getString("remotehost");
        remoteport = jsonConfig.getInt("remoteport");
        name = jsonConfig.getString("name");
        useheartbeat = jsonConfig.getString("useheartbeat");
        heartbeattime = jsonConfig.getInt("heartbeattime");
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public String getRemoteHost() {
        return remotehost;
    }

    public int getRemotePort() {
        return remoteport;
    }

    public String getName() {
        return name;
    }

    public String getUseHeartBeat() { return useheartbeat; }

    public int getHeartBeatTime() { return heartbeattime; }
}
